package controller

import (
	"blog_server/global"
	"blog_server/internal/model/entity"
	"blog_server/internal/model/request"
	"blog_server/internal/service"
	"blog_server/pkg/app"
	"blog_server/pkg/errcode"
	"github.com/gin-gonic/gin"
	"strconv"
)

type CategoryController struct{}

func NewCategoryController() CategoryController {
	return CategoryController{}
}

func (cc *CategoryController) GetAll(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	classList, err := svc.GetAllCategory()
	if err != nil {
		global.Logger.Errorf("svc.GetAllCategory err: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(gin.H{"list": classList})
}

func (cc CategoryController) Create(c *gin.Context) {
	response := app.NewResponse(c)
	param := &entity.Category{}
	err := c.ShouldBind(param)
	if err != nil {
		global.Logger.Errorf("app.BindAndValid errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	svc := service.New(c.Request.Context())
	err = svc.CreateCategory(param)
	if err != nil {
		global.Logger.Errorf("svc.CreateCategory err:%v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(nil)
}

func (cc *CategoryController) GetByPagination(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	pageSize, _ := app.GetPageSize(c)
	page, _ := app.GetPage(c)
	res, cnt, err := svc.GetCategoriesByPagination(page, pageSize)
	if err != nil {
		global.Logger.Errorf("svc.GetCategoriesByPagination err: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(gin.H{"list": res, "cnt": cnt})
}

func (cc *CategoryController) Update(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	res := c.Param("id")
	id, err := strconv.Atoi(res)
	if err != nil {
		global.Logger.Errorf("strconv.Atoi errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	param := request.CategoryRequest{}
	err = c.ShouldBind(&param)
	if err != nil {
		global.Logger.Errorf("ShouldBind errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	err = svc.UpdateCategory(uint(id), &param)
	if err != nil {
		global.Logger.Errorf("svc.UpdateCategory errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(nil)
}

func (cc *CategoryController) Delete(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	res := c.Param("id")
	id, err := strconv.Atoi(res)
	if err != nil {
		global.Logger.Errorf("strconv.Atoi errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	err = svc.DeleteCategory(uint(id))
	if err != nil {
		global.Logger.Errorf("svc.DeleteCategory errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(nil)
}

func (cc *CategoryController) GetBySearchText(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	searchText := c.Param("search_text")
	res, err := svc.GetCategoriesBySearchText(searchText)
	if err != nil {
		global.Logger.Errorf("svc.GetCategoriesBySearchText errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(gin.H{"list": res})
}

func (cc *CategoryController) GetCategoriesAndTags(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	categoriesList, err := svc.GetAllCategory()
	if err != nil {
		global.Logger.Errorf("svc.GetAllCategory err: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	tagList, err := svc.GetAllTags()
	if err != nil {
		global.Logger.Errorf("svc.GetAllTags err: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(gin.H{"categories": categoriesList, "tags": tagList})

}
